package com.mofofa.maozy.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.data.RevObjInfoPageFieldConfig
import com.mofofa.maozy.databinding.ViewMaozyFormSelectCreditReportBinding

class SelectCreditReportForm : RelativeLayout,
    com.mofofa.maozy.components.form.BaseFormAction, BaseFormAction {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyFormSelectCreditReportBinding
    private lateinit var vMaozyRootMaozyView: View

    private var mMaozyIsMaozyCheck = false

    private var acfscrMaozyTitle: String? = null
    private var acfscrMaozyDetail: String? = null

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null
    private var mMaozyFormMaozyConfig: RevObjInfoPageFieldConfig? = null
    private var mMaozyOnMaozyStateMaozyChangedMaozyListener: (Int, String) -> Unit =
        { action, content -> }

    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vMaozyRootMaozyView = View.inflate(it, R.layout.view_maozy_form_select_credit_report, this)
            mMaozyViewMaozyBinding = ViewMaozyFormSelectCreditReportBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.vFormSelectCreditReportAction.setOnClickListener {
                mMaozyIsMaozyCheck = !mMaozyIsMaozyCheck
                setMaozyCheck(mMaozyIsMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    fun setMaozyCheck(check: Boolean) {
        mMaozyIsMaozyCheck = check
        if (mMaozyIsMaozyCheck) {
            mMaozyViewMaozyBinding.imageFormSelectCreditReportCheck.setImageResource(R.drawable.maozy_checkbox_green_yes)
        } else {
            mMaozyViewMaozyBinding.imageFormSelectCreditReportCheck.setImageResource(R.drawable.maozy_checkbox_no_small)
        }
        mMaozyFormMaozyConfig?.fillContent = getMaozyContent()
    }

    fun getMaozyContent(): String {
        if (mMaozyIsMaozyCheck) return "YES"
        else return "NO"
    }

    override fun isMaozyFormMaozyCompleted(): Boolean {
        if (!mMaozyIsMaozyCheck) {
            mMaozyViewMaozyBinding.rlFormSelectCreditReportContentBg.setBackgroundResource(R.drawable.maozy_form_input_error)
            return false
        }

        mMaozyViewMaozyBinding.rlFormSelectCreditReportContentBg.setBackgroundResource(R.drawable.maozy_form_credit_report)
        return true
    }

    override fun isMaozyFormMaozyCompletedMaozySilent(): Boolean {
        return mMaozyIsMaozyCheck
    }

    override fun setMaozyOnMaozyStateMaozyChangedMaozyListener(listener: (Int, String) -> Unit) {
        mMaozyOnMaozyStateMaozyChangedMaozyListener = listener
    }

    override fun setMaozyTitle(title: String) {
        acfscrMaozyTitle = title
        mMaozyViewMaozyBinding.textFormSelectCreditReportTitle.text = (acfscrMaozyTitle)
    }

    override fun setMaozyHint(hint: String) {
        acfscrMaozyDetail = hint
        mMaozyViewMaozyBinding.textFormSelectCreditReportDetail.text = (acfscrMaozyDetail)
    }

    override fun setMaozyFormMaozyConfig(item: RevObjInfoPageFieldConfig) {
        mMaozyFormMaozyConfig = item
        setMaozyTitle(item.title.toString())
        setMaozyHint(item.hint.toString())
        tag = item.paramName
        item.fillContent?.let { fillContent ->
            setMaozyCheck(fillContent.equals("YES", true))
        }
        if (item.needCheckComplete == true) {
            if (item.isMust?.equals("YES") == true) {
                isMaozyFormMaozyCompleted()
            }
        }
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }

    override fun setMaozyDetail(detail: String) {

    }

    override fun setMaozyContent(content: String) {

    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }
}